from django.contrib.auth.models import Permission, ContentType, Group
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import  ModelViewSet
from mg_admin.utils import PageNumPagination
from mg_admin.serializers.system import PermissionSerializer,ContentTypeSerializer,GroupSerializer,AdminSerializer
from userapp.models import Users



class PermissionsAPIView(ModelViewSet):
    pagination_class = PageNumPagination
    queryset = Permission.objects.order_by('-id')
    serializer_class = PermissionSerializer

    @action(methods=['get'],detail=False)
    def content_types(self,request):
        # 自定义动作方法 获取所有数据
        queryset = ContentType.objects.order_by('-id')
        ser = ContentTypeSerializer(queryset,many=True)
        return Response(ser.data)


class GroupsAPIView(ModelViewSet):
    pagination_class = PageNumPagination
    queryset = Group.objects.order_by('-id')
    serializer_class = GroupSerializer


class AdminsAPIView(ModelViewSet):
    pagination_class = PageNumPagination
    queryset = Users.objects.filter(is_staff=True).order_by('-id')
    serializer_class = AdminSerializer
